<?php

// app/Http/Controllers/AuthController.php

namespace App\Http\Controllers;

use App\Http\Requests\RegisterRequest;
use App\Http\Requests\LoginRequest;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Request;

class AuthController extends Controller
{


    public function register(RegisterRequest $request)
//    :JsonResponse
    {

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);
        return response()->json(['message' => '注册成功。', 'user' => $user], 201);
//        return view('result');
    }



    public function login(LoginRequest $request): JsonResponse


    {

//        dump(Auth::attempt($request->only('email', 'password')));
        if (Auth::attempt($request->only('email', 'password'))) {

            $user = Auth::user();

            return response()->json(['message' => '登录成功。', 'user' => $user]);
        }


        return response()->json(['message' => '邮箱或密码错误。'], 401);
    }


    //master分支
    //master A
    //temp分支
    //temp B
}
